package sicau.car.config;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import sicau.car.utill.TokenUtil;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * @Author ITcz
 * @Data 2020-09-16 - 11:32
 *
 * 检查管理员是否登录，没有则返回登录页面
 */
@Component
public class AdminIntercepter extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        //如果token为空或者token值与服务端值不对应则跳转至登录页面
        if(token ==null || !(TokenUtil.containsToken(token))) {
            request.setAttribute("msg", "请先登录");
            String redirectURL=URLEncoder.encode("http://113.54.11.32:8080/SICAU/adminCar/index/login.html");
            response.sendRedirect(redirectURL);
            return false;
        }
        return true;
    }
}
